package cn.iocoder.yudao.module.bpm.message.rabbitmq;

import cn.iocoder.yudao.module.bpm.message.ProcessTaskMessage;
import cn.iocoder.yudao.module.bpm.message.dto.BpmProcessTaskAssignedMessage;
import cn.iocoder.yudao.module.bpm.message.dto.BpmProcessTaskCancelledMessage;
import cn.iocoder.yudao.module.bpm.message.dto.BpmProcessTaskCompletedMessage;
import cn.iocoder.yudao.module.bpm.message.dto.BpmProcessTaskCreatedMessage;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

@Log4j2
public class RabbitMqProcessTaskMessage implements ProcessTaskMessage {

    private final RabbitTemplate rabbitTemplate;

    private final TopicExchange processInstanceTaskCreatedTopicExchange;
    private final TopicExchange processInstanceTaskAssignedTopicExchange;
    private final TopicExchange processInstanceTaskCleanTopicExchange;
    private final TopicExchange processInstanceTaskFinishedTopicExchange;

    public RabbitMqProcessTaskMessage(
            RabbitTemplate rabbitTemplate,
            TopicExchange processInstanceTaskCreatedTopicExchange,
            TopicExchange processInstanceTaskAssignedTopicExchange,
            TopicExchange processInstanceTaskCleanTopicExchange,
            TopicExchange processInstanceTaskFinishedTopicExchange
    ) {
        this.rabbitTemplate = rabbitTemplate;
        this.processInstanceTaskCreatedTopicExchange = processInstanceTaskCreatedTopicExchange;
        this.processInstanceTaskAssignedTopicExchange = processInstanceTaskAssignedTopicExchange;
        this.processInstanceTaskCleanTopicExchange = processInstanceTaskCleanTopicExchange;
        this.processInstanceTaskFinishedTopicExchange = processInstanceTaskFinishedTopicExchange;
    }
    @Override
    public void onProcessInstanceTaskCreatedMsg(BpmProcessTaskCreatedMessage bpmProcessTaskCreatedMessage) {
        rabbitTemplate.convertAndSend(processInstanceTaskCreatedTopicExchange.getName(), null, bpmProcessTaskCreatedMessage);
    }

    @Override
    public void onProcessInstanceTaskAssignedMsg(BpmProcessTaskAssignedMessage bpmProcessTaskAssignedMessage) {
        rabbitTemplate.convertAndSend(processInstanceTaskAssignedTopicExchange.getName(), null, bpmProcessTaskAssignedMessage);
    }

    @Override
    public void onProcessInstanceTaskCleanMsg(BpmProcessTaskCancelledMessage bpmProcessTaskCancelledMessage) {
        rabbitTemplate.convertAndSend(processInstanceTaskCleanTopicExchange.getName(), null, bpmProcessTaskCancelledMessage);
    }

    @Override
    public void onProcessInstanceTaskFinishedMsg(BpmProcessTaskCompletedMessage bpmProcessTaskCompletedMessage) {
        rabbitTemplate.convertAndSend(processInstanceTaskFinishedTopicExchange.getName(), null, bpmProcessTaskCompletedMessage);
    }
}
